package com.bw.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bw.bean.Cate;
import com.bw.bean.Movie;
import com.bw.service.MovieService;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;

@Controller
public class Maincontroller {

	
	@Resource
	private MovieService service;
	@RequestMapping("/findall.do")
	public String findall(Model m ,String mname) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("mname", mname);
		List<Map<String,Object>> list=service.findall(map);
		m.addAttribute("list", list);
		m.addAttribute("map", map);
		return "list";
		
	}
	
	@RequestMapping("/findcate.do")
	public String findcate(Model m){
		List<Cate> list=service.findcate();

		m.addAttribute("list", list);
		return "add";
	}
	@ResponseBody
	@RequestMapping("/add.do")
	public int add(Movie m ,String cids ) {

		List<Integer> list=new ArrayList<Integer>();
		String[] split = cids.split(",");
		for (String string : split) {
			list.add(Integer.parseInt(string+""));
		}
		
		
		
		System.out.println(cids);
		int n=service.add(m,list);
		
	
		
		return n;
		
	}

	
	@RequestMapping("/toupdate.do")
	public String toupdate(int mid,Model m) {
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("mid", mid);
		List<Map<String, Object>> findall = service.findall(map);
	
		
		
		
		List<Cate> list=service.findcate();
		
		for (Cate cate : list) {
			for (Map<String, Object> map2 : findall) {
				
				if(cate.getCid()==map2.get("cids")) {
					cate.setStus(1);
				}
			}
			
		}
		m.addAttribute("m", findall);
		
		
	
	
		m.addAttribute("list", list);
		
		
		return "toupdate";
		
	}
	
	
	
	
	
	
	

}
